package org.dromara.crawler.mongo.mongoservice.impl;

import com.mongoplus.aggregate.Aggregate;
import com.mongoplus.aggregate.AggregateWrapper;
import com.mongoplus.mapping.TypeReference;
import com.mongoplus.service.impl.ServiceImpl;
import org.dromara.crawler.mongo.mongodomain.ZhEntity;
import org.dromara.crawler.mongo.mongoservice.HistoricalDataService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HistoricalDataServiceImpl extends ServiceImpl<ZhEntity> implements HistoricalDataService {


    @Override
    public Map<String, Object> statistics() {
        Map<String,Object> map = new HashMap<>();
        AggregateWrapper zhWrapper = new AggregateWrapper()
            .count("total");
        List<Map<String, Object>> zhCount =execute("zh", zhWrapper);

        AggregateWrapper enWrapper = new AggregateWrapper()
            .count("total");
        List<Map<String, Object>> enCount =execute("en", zhWrapper);
        map.put("zh", zhCount.get(0).get("total"));
        map.put("en", enCount.get(0).get("total"));
        return map;
    }


    private List<Map<String, Object>> execute(String collectionName, Aggregate<?> aggregate) {
        return baseMapper.aggregateList(collectionName, aggregate, new TypeReference<Map<String, Object>>() {
        });
    }

}
